package com.nlp.trie.tel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map.Entry;


/**
 * 数字搜索树
 * @author ygsong.abcft
 *
 */
public class TrieTree {
	
	private TrieNode root;
	
	public TrieTree() {
		this.root = new TrieNode('0');
	}
	
	//把词加入词典
	private void addWord(String word, TrieNode root, String area) {
		TrieNode currentNode = root;
		for(int i = 1; i < word.length(); i++) {
			char c0 = word.charAt(i);
			/*int ind = Integer.parseInt(word.substring(i, i+1));
			if (null == currentNode.children[ind]) {
				currentNode.children[ind] = new TrieNode(c0);
			}
			currentNode = currentNode.children[ind];*/
			if (null == currentNode.children.get(c0)) {
				currentNode.children.put(c0, new TrieNode(c0));
			}
			currentNode = currentNode.children.get(c0);
		}
		currentNode.area = area;
	}
	
	/**
	 * 加载配置文件
	 * @param dicPath
	 * @throws IOException
	 */
	public void load(String dicPath) throws IOException {
		BufferedReader br = null;
		try {
			InputStream file = TrieTree.class.getResourceAsStream("/"+dicPath);

			br= new BufferedReader(new InputStreamReader(file, "UTF-8"));
			String data;

			while((data = br.readLine()) != null) {
				String[] strs = data.trim().split(":");
				String area = strs[1]+":"+strs[2];
				addWord(strs[0], root, area);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			br.close();
		}
	}
	
	/**
	 * 根据号码搜索区域
	 * @param tel
	 * @return
	 */
	public String search(String tel) {
		TrieNode currentNode = root;
		for (int i = 1; i < tel.length(); i++) {
			currentNode = currentNode.children.get(tel.charAt(i));//currentNode.children[(tel.charAt(i)-'0')];
			if (null != currentNode && currentNode.area != null) {
				return currentNode.area;
			}
			if (currentNode == null) {
				return null;
			}
		}
		return null;
	}
	
	/**
	 * 打印树结构
	 */
	@Override
	public String toString() {
		StringBuilder ret = new StringBuilder();
		ArrayList<TrieNode> parentNodes = new ArrayList<TrieNode>();
		deepSearch(parentNodes,root, ret, 0);
		return ret.toString();
	}
	public void deepSearch(ArrayList<TrieNode> parentNodes,TrieNode node, StringBuilder ret, int deapth) {
		if (node != null) {
			for (int i = 0; i < deapth; i++) {
				ret.append("| ");
			}
			ret.append(node.getPath(parentNodes)+"\n");
			@SuppressWarnings("unchecked")
			ArrayList<TrieNode> newParent = (ArrayList<TrieNode>) parentNodes.clone();
			newParent.add(node);
			/*for (TrieNode c : node.children) {
				deepSearch(parentNodes, c, ret, deapth+1);
			}*/
			for (Entry<Character,TrieNode> entry : node.children.entrySet()) {
				deepSearch(parentNodes, entry.getValue(), ret, deapth+1);
			}
		}
	}
	
	public static void main(String[] args) throws IOException {
		TrieTree tree = new TrieTree();
		//tree.load("E:\\search\\nlp\\trie\\src\\main\\resources\\tel");
		tree.load("tel");
		System.out.println(tree.toString());
		String area = tree.search("0795");
		System.out.println(area);
	}

}
